package com.lunwen.lunwen.controller;

import com.lunwen.lunwen.bean.DaoShi;
import com.lunwen.lunwen.bean.Result;
import com.lunwen.lunwen.bean.XueSheng;
import com.lunwen.lunwen.bean.ZhuanYe;
import com.lunwen.lunwen.config.ShiroUtils;
import com.lunwen.lunwen.services.DaoShiService;
import com.lunwen.lunwen.services.SysUserService;
import com.lunwen.lunwen.services.XueYuanService;
import com.lunwen.lunwen.services.ZhuanYeService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * (DaoShi)表控制层
 *
 * @author makejava
 * @since 2020-07-20 12:25:07
 */
@RestController
@RequestMapping("api")
public class DaoShiController {

    /**
     * 服务对象
     */
    @Autowired
    private DaoShiService daoShiService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private XueYuanService xueYuanService;

    @Autowired
    private ZhuanYeService zhuanYeService;

    @RequestMapping("registdaoshi")
    public Result registDaoShi(@RequestBody DaoShi daoShi){
        Result result = new Result();
        if (StringUtils.isEmpty(daoShi.getSysUser().getUsername())){
            result.setResult("201","用户名不能为空！");
            return result;
        }
        if (StringUtils.isEmpty(daoShi.getSysUser().getPassword())){
            result.setResult("201","用户密码不能为空！");
            return result;
        }
        if (sysUserService.queryByName(daoShi.getSysUser().getUsername()) != null){
            result.setResult("201","该用户已存在！");
            return result;
        }

        if (!StringUtils.isEmpty(daoShi.getXueyuanId())){
            if (xueYuanService.queryById(daoShi.getXueyuanId()) == null){
                result.setResult("201","该学院不存在！");
                return result;
            }
        }
        if (!StringUtils.isEmpty(daoShi.getZhuanyeId())){
            if (zhuanYeService.queryById(daoShi.getZhuanyeId()) == null){
                result.setResult("201","该专业不存在！");
                return result;
            }
            if (!zhuanYeService.queryById(daoShi.getZhuanyeId()).getXueyuanId().equals(daoShi.getXueyuanId())){
                result.setResult("201","该学院不存在该专业！");
                return result;
            }
        }

        String id = UUID.randomUUID().toString();
        daoShi.setId(id);
        daoShi.getSysUser().setSalt("123");
        daoShi.getSysUser().setPassword(ShiroUtils.sha256(daoShi.getSysUser().getPassword(),daoShi.getSysUser().getSalt()));
        daoShi.getSysUser().setStatus(1);
        daoShi.getSysUser().setType(0);
        daoShi.getSysUser().setCreateUserId(0l);
        daoShi.getSysUser().setCreateTime(new Date());
        DaoShi insert = daoShiService.insert(daoShi);
        result.setResult("200","注册成功！",insert);
        return result;
    }

    @RequestMapping("selectalldaoshi")
    public Result selectAllDaoShi(@RequestParam("yeMa") int yeMa,
                                  @RequestParam(required = false) String xueyuanId,
                                  @RequestParam(required = false) String zhuanyeId,
                                  @RequestParam(required = false) String zhicheng,
                                  @RequestParam(required = false) String zhiwu,
                                  @RequestParam(required = false,defaultValue = "-1") int leixing){
        Result result = new Result();
        DaoShi daoShi = new DaoShi();
        daoShi.setXueyuanId(xueyuanId);
        daoShi.setZhuanyeId(zhuanyeId);
        daoShi.setZhicheng(zhicheng);
        daoShi.setZhiwu(zhiwu);
        daoShi.setLeixing(leixing);
        if (!StringUtils.isEmpty(daoShi.getXueyuanId())){
            if (xueYuanService.queryById(daoShi.getXueyuanId()) == null){
                result.setResult("201","该学院不存在！");
                return result;
            }
        }
        if (!StringUtils.isEmpty(daoShi.getZhuanyeId())){
            if (zhuanYeService.queryById(daoShi.getZhuanyeId()) == null){
                result.setResult("201","该专业不存在！");
                return result;
            }
            if (!zhuanYeService.queryById(daoShi.getZhuanyeId()).getXueyuanId().equals(daoShi.getXueyuanId())){
                result.setResult("201","该学院不存在该专业！");
                return result;
            }
        }
        int limit = 10;
        int zongGeShu = daoShiService.queryCount(daoShi);
        int xiaBiao = (yeMa - 1) * limit;
        int zongYeMa = (zongGeShu - 1) / limit + 1;
        List<DaoShi> daoShis = daoShiService.queryAllByLimit(daoShi, xiaBiao, limit);
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("zongGeShu", zongGeShu);
        map.put("zongYeMa", zongYeMa);
        map.put("list", daoShis);
        result.setResult("200","查询成功！",map);
        return result;
    }

    @RequestMapping("updatedaoshi")
    public Result updateDaoShi(@RequestBody DaoShi daoShi) {
        Result result = new Result();
        if (StringUtils.isEmpty(daoShi.getUserId())){
            result.setResult("201","用户ID不能为空！");
            return result;
        }
        if (daoShiService.queryByUserId(daoShi.getUserId()) == null){
            result.setResult("201","该导师不存在！");
            return result;
        }
        if (!StringUtils.isEmpty(daoShi.getSysUser().getUsername())){
            if (sysUserService.queryByName(daoShi.getSysUser().getUsername()) != null){
                result.setResult("201","用户名已存在！");
                return result;
            }
        }
        if (!StringUtils.isEmpty(daoShi.getXueyuanId())){
            if (xueYuanService.queryById(daoShi.getXueyuanId()) == null){
                result.setResult("201","该学院不存在！");
                return result;
            }
        }
        if (!StringUtils.isEmpty(daoShi.getZhuanyeId())){
            if (zhuanYeService.queryById(daoShi.getZhuanyeId()) == null){
                result.setResult("201","该专业不存在！");
                return result;
            }
            if (!zhuanYeService.queryById(daoShi.getZhuanyeId()).getXueyuanId().equals(daoShi.getXueyuanId())){
                result.setResult("201","该学院不存在该专业！");
                return result;
            }
        }
        if (!StringUtils.isEmpty(daoShi.getSysUser().getPassword())){
            daoShi.getSysUser().setPassword(ShiroUtils.sha256(daoShi.getSysUser().getPassword(),"123"));
        }
        if (daoShi.getSysUser().getStatus() != null){
            if (daoShi.getSysUser().getStatus() != 0L && daoShi.getSysUser().getStatus() != 1L){
                result.setResult("201","状态信息错误！");
                return result;
            }
        }
        DaoShi update = daoShiService.update(daoShi);
        result.setResult("200","修改成功！",update);
        return result;
    }


}